//
//  SmarllPosterView.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/25.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "SmarllPosterView.h"
#define kWidth  [UIScreen mainScreen].bounds.size.width
#define kHieght [UIScreen mainScreen].bounds.size.height

@implementation SmarllPosterView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    
    if (self) {
        
        self.dataSource = self;
        self.delegate = self;
        
        [self registerClass:[SmalPosterViewCelll class] forCellWithReuseIdentifier:@"smallPosterViewCell"];
    }
    
    return self;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_modalArray count];
}


#pragma mark - 滑动定位
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
    
    CGFloat x = targetContentOffset->x;
    NSInteger i = x/((kWidth - 60)/5);
    if (i <= 0)
    {
        i = 0;
    }
    else if(i >= [_modalArray count])
    {
        i = [_modalArray count]-1;
    }
    _currentlyIndex = i;
    targetContentOffset->x =  ((kWidth - 60)/5 + 10 )*i;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row != _currentlyIndex) {
        
        [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        self.currentlyIndex = indexPath.row;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SmalPosterViewCelll* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallPosterViewCell" forIndexPath:indexPath];
    
    cell.modal = _modalArray[indexPath.row];

    
    return cell;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, kWidth/2 - ((kWidth - 60)/10), 0,  kWidth/2 - ((kWidth - 60)/10));
}


@end
